package com.zhang;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URLClassLoader;

public class DynamicJarApp {
    final static String libDir = "d://lib";
    final static String testClass = "cn.test.TestClass";
    static URLClassLoader currentClassload;
    static long changeLastModified=0;
 
    /**
     * 根据修改时间判断文件是否修改
     * @param libDir
     * @return
     */
    public static long changeJarVersion(String libDir) {
        long lastModified = 0;
        File base = new File(libDir);
        if (null != base && base.canRead() && base.isDirectory()) {
            File[] files = base.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    if (pathname.getName().contains(".jar")) {
                        return true;
                    } else return false;
                }
            });
            for (int j = 0; j < files.length; j++) {
                lastModified += files[j].lastModified();
            }
        }
        return lastModified;
    }
 
    public static void main(String[] args) {
        Thread thead=null;
        try {
            changeLastModified=changeJarVersion(libDir);
            currentClassload= new DynamicJarClassLoader(libDir, null);
            Class<?> clazz = currentClassload.loadClass(testClass);
            System.out.println(clazz.getName());
            Object object= clazz.newInstance();
            Method method= clazz.getDeclaredMethod("info");
            method.setAccessible(true);
            method.invoke(object);
 
            thead=new Thread(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        try {
                            System.out.println("wait......");
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
 
                        long lastModified = changeJarVersion(libDir);
 
                        //jar包版本是否修改
                        if (changeLastModified != lastModified) {
                            try {
                                //关闭旧的类加载器
                                currentClassload.close();
                                System.out.println("close......");
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            finally {
                                changeLastModified = lastModified;
                            }
                            //创建新的类加载器
                            DynamicJarClassLoader newDynamicJarClassLoader = new DynamicJarClassLoader(libDir, currentClassload.getParent());
                            currentClassload = newDynamicJarClassLoader;
                        }
 
                        try {
                            //执行jar包类方法
                            Class<?> clazz = currentClassload.loadClass(testClass);
                            System.out.println(clazz.getName());
                            Object object = clazz.newInstance();
                            Method method = clazz.getDeclaredMethod("info");
                            method.setAccessible(true);
                            method.invoke(object);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            thead.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
        try {
            thead.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }       
    }
}